package com.lipiao.makerandroid.View.Adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lipiao.makerandroid.Bean.RespondBean.ReadBean;
import com.lipiao.makerandroid.R;
import java.util.List;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ContactViewHolder>{

    private List<ReadBean> readBeanList;

    public CollectionAdapter(List<ReadBean> readBeanList) {
        this.readBeanList = readBeanList;
    }
    @NonNull
    @Override
    public CollectionAdapter.ContactViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.article_card_item, viewGroup, false);
        System.out.println("onCreateViewHolder");
        return new CollectionAdapter.ContactViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CollectionAdapter.ContactViewHolder contactViewHolder, int i) {
        //通过其get()方法可以获得其中的对象
        ReadBean readBean=readBeanList.get(i);
        contactViewHolder.author.setText(readBean.getUserNumber());
        contactViewHolder.title.setText(readBean.getReadText());
        contactViewHolder.time.setText(""+readBean.getReadId());
        contactViewHolder.kind.setText(""+readBean.getReadId());
        contactViewHolder.article.setOnClickListener(v -> {
            //触发自定义监听的单击事件
            onItemClickListener.onItemClick(contactViewHolder.itemView, i);
        });
        contactViewHolder.article.setOnLongClickListener(v -> {
            //触发自定义监听的单击事件
            onItemClickListener.onItemLongClick(contactViewHolder.itemView, i);
            return true;
        });
    }

    private CollectionAdapter.OnItemClickListener onItemClickListener;
    @Override
    public int getItemCount() {

        System.out.println(readBeanList.size());
        return readBeanList.size();
    }
    class ContactViewHolder extends RecyclerView.ViewHolder {
        //create the viewHolder class
        protected CardView article;
        protected TextView author;
        protected TextView title;
        protected TextView time;
        protected TextView kind;

        public ContactViewHolder(View itemView) {
            super(itemView);
            article=itemView.findViewById(R.id.cv_top_article_card);
            author = itemView.findViewById(R.id.tv_top_article_author);
            title = itemView.findViewById(R.id.tv_top_article_title);
            time = itemView.findViewById(R.id.tv_top_article_time);
            kind = itemView.findViewById(R.id.tv_top_article_kind);
        }

    }
    public void setOnItemClickListener(CollectionAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setReadBeanList(List<ReadBean> readBeanList) {
        this.readBeanList = readBeanList;
    }
}
